package com.tps.cloud.system.constraint;

import cn.hutool.core.util.StrUtil;
import com.tps.cloud.system.service.SystemUserService;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import lombok.AllArgsConstructor;

/**
 * 自定义注解，用于验证用户账号唯一
 */
@AllArgsConstructor
public class UniqueUsernameValidator implements ConstraintValidator<UniqueUsername, String> {


    private final SystemUserService systemUserService;


    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StrUtil.isEmpty(value)) {
            return true;
        }
        return systemUserService.findByUsername(value) == null;
    }
}

